From: tsteven4 Date: Sat, 9 Feb 2019 22:43:03 +0000 (-0700) Subject: Use QList instead of QueueList to back RouteList. X-Git-Tag: archive/raspbian/1.10.0+ds-2+rpi1~1^2~12^2~8^2~22^2~2 X-Git-Url: https://dgit.raspbian.org/%22http:/www.example.com/cgi/%22https://%22Program/%22http:/www.example.com/cgi/%22https:/%22Program?a=commitdiff_plain;h=748132f3022c3fb767b6bf8f2405730a53407e0c;p=gpsbabel.git Use QList instead of QueueList to back RouteList. This eliminates the usage of the legacy queue code for route lists. --- diff --git a/defs.h b/defs.h index b9710d6e6..f8cb68f39 100644 --- a/defs.h +++ b/defs.h @@ -652,7 +652,6 @@ struct computed_trkdata { class route_head { public: - queue Q; /* Link onto parent list. */ queue waypoint_list; /* List of child waypoints */ QString rte_name; QString rte_desc; @@ -679,17 +678,11 @@ typedef void (*route_hdr)(const route_head*); typedef void (*route_trl)(const route_head*); // TODO: Consider using composition instead of private inheritance. -class RouteList : private QueueList +class RouteList : private QList { public: -// FIXME: The interface should NOT depend on the implementation of the list. -// Migrate to std::sort using a compare function -// typedef bool (*Compare)(const route_head* a, const route_head* b); - typedef int (*Compare)(const queue* a, const queue* b); + typedef bool (*Compare)(const route_head* a, const route_head* b); - RouteList(); - - int count() const; // a.k.a. size() int waypt_count() const; void add_head(route_head* rte); // a.k.a. append(), push_back() // FIXME: Generally it is inefficient to use an element pointer or reference to define the element to be deleted, use iterator instead, @@ -719,19 +712,20 @@ public: // Our contained element (route_head) also contains a container (waypoint_list), // and we maintain a total count the elements in these contained containers, i.e. // the total number of waypoints in all the routes in the RouteList. - using QueueList::begin; - using QueueList::end; - using QueueList::cbegin; - using QueueList::cend; - using QueueList::empty; // a.k.a. isEmpty() - using QueueList::front; // a.k.a. first() - using QueueList::back; // a.k.a. last() - using QueueList::Iterator; - using QueueList::ConstIterator; + using QList::begin; + using QList::end; + using QList::cbegin; + using QList::cend; + using QList::empty; // a.k.a. isEmpty() + using QList::front; // a.k.a. first() + using QList::back; // a.k.a. last() + using QList::count; // a.k.a. size() + using QList::iterator; + using QList::const_iterator; + typedef iterator Iterator; + typedef const_iterator ConstIterator; private: - queue head; - int head_ct{0}; int waypt_ct{0}; }; @@ -789,10 +783,7 @@ template void RouteList::disp_all(T1 rh, T2 rt, T3 wc) { - queue* elem, *tmp; - QUEUE_FOR_EACH(&head, elem, tmp) { - const route_head* rhp; - rhp = reinterpret_cast(elem); + foreach (const route_head* rhp, *this) { // rh != nullptr, caught with an overload of common_disp_all rh(rhp); route_disp(rhp, wc); @@ -805,10 +796,7 @@ template void RouteList::disp_all(std::nullptr_t /* rh */, T2 rt, T3 wc) { - queue* elem, *tmp; - QUEUE_FOR_EACH(&head, elem, tmp) { - const route_head* rhp; - rhp = reinterpret_cast(elem); + foreach (const route_head* rhp, *this) { // rh == nullptr route_disp(rhp, wc); // rt != nullptr, caught with an overload of common_disp_all @@ -820,10 +808,7 @@ template void RouteList::disp_all(T1 rh, std::nullptr_t /* rt */, T3 wc) { - queue* elem, *tmp; - QUEUE_FOR_EACH(&head, elem, tmp) { - const route_head* rhp; - rhp = reinterpret_cast(elem); + foreach (const route_head* rhp, *this) { // rh != nullptr, caught with an overload of common_disp_all rh(rhp); route_disp(rhp, wc); @@ -835,10 +820,7 @@ template void RouteList::disp_all(std::nullptr_t /* rh */, std::nullptr_t /* rt */, T3 wc) { - queue* elem, *tmp; - QUEUE_FOR_EACH(&head, elem, tmp) { - const route_head* rhp; - rhp = reinterpret_cast(elem); + foreach (const route_head* rhp, *this) { // rh == nullptr route_disp(rhp, wc); // rt == nullptr diff --git a/gtm.cc b/gtm.cc index 836010918..c5ab84729 100644 --- a/gtm.cc +++ b/gtm.cc @@ -19,11 +19,14 @@ */ /* - * Documentation can be found at http://www.trackmaker.com/download/ref_guide_eng.pdf + * Documentation can be found at + * https://www.trackmaker.com/download/ref_guide_eng.pdf + * https://www.trackmaker.com/download/GTM211_format.pdf */ #include "defs.h" #include "jeeps/gpsmath.h" +#include static gbfile* file_in, *file_out; static int indatum; @@ -507,11 +510,10 @@ gtm_wr_deinit() static void gtm_read() { - route_head* first_trk_head = nullptr; route_head* trk_head = nullptr; route_head* rte_head = nullptr; Waypoint* wpt; - int real_tr_count = 0; + QList real_track_list; unsigned int icon; int i; @@ -575,20 +577,28 @@ gtm_read() if (start_new || !trk_head) { trk_head = route_head_alloc(); track_add_head(trk_head); - real_tr_count++; - if (!first_trk_head) { - first_trk_head = trk_head; - } + real_track_list.append(trk_head); } track_add_wpt(trk_head, wpt); } /* Tracklog styles */ - trk_head = first_trk_head; - for (i = 0; i != ts_count && i != real_tr_count; i++) { - trk_head->rte_name = fread_string(file_in); + // TODO: The format document states there are ts_count tracklog style entries, + // and tr_count tracklog entries. + // Some tracklog entries may be contiuation entries, so we turn these into + // real_track_list.size() <= tr_count tracks. + // If ts_count != real_track_list.size() we don't know how to line up the tracklogs, + // and the real tracks, with the tracklog styles. + if (ts_count != real_track_list.size()) { + warning(MYNAME ": The number of tracklog entries with the new flag set doesn't match the number of tracklog style entries."); + } + for (i = 0; i != ts_count; i++) { + QString tname = fread_string(file_in); fread_discard(file_in, 12); - trk_head = reinterpret_castQUEUE_NEXT(&trk_head->Q); + if (i < real_track_list.size()) { + trk_head = real_track_list.at(i); + trk_head->rte_name = tname; + } } /* Routes */ diff --git a/route.cc b/route.cc index 62ac2b429..2c5c03319 100644 --- a/route.cc +++ b/route.cc @@ -19,12 +19,15 @@ #include "defs.h" #include "grtcirc.h" // for RAD, gcdist, heading_true_degrees, radtometers -#include "queue.h" // for queue, dequeue, QUEUE_FOR_EACH, QUEUE_MOVE, QUEUE_INIT, sortqueue, ENQUEUE_TAIL, QUEUE_EMPTY, ENQUEUE_AFTER, ENQUEUE_HEAD, QUEUE_LAST, QUEUE_NEXT, QueueList +#include "queue.h" // for dequeue, queue, QUEUE_FOR_EACH, QUEUE_EMPTY, ENQUEUE_HEAD, ENQUEUE_TAIL, QUEUE_INIT, QUEUE_LAST, QUEUE_NEXT #include "session.h" // for curr_session, session_t (ptr only) #include "src/core/datetime.h" // for DateTime #include "src/core/optional.h" // for optional, operator>, operator< #include // for QDateTime +#include // for QList<>::iterator #include // for QString +#include // for foreach +#include // for sort #include // for nullptr_t RouteList* global_route_list; @@ -103,37 +106,6 @@ track_insert_head(route_head* rte, route_head* predecessor) global_track_list->insert_head(rte, predecessor); } -// FIXME: can we delete this unused and untested code? -#ifdef DEAD_CODE_IS_REBORN -static -route_head* -common_route_by_name(queue* routes, const char* name) -{ - queue* elem, *tmp; - - QUEUE_FOR_EACH(routes, elem, tmp) { - route_head* rte = reinterpret_cast(elem); - if (rte->rte_name == name) { - return rte; - } - } - - return nullptr; -} - -route_head* -route_find_route_by_name(const char* name) -{ - return common_route_by_name(&my_route_head, name); -} - -route_head* -route_find_track_by_name(const char* name) -{ - return common_route_by_name(&my_track_head, name); -} -#endif - void route_add_wpt(route_head* rte, Waypoint* wpt, const QString& namepart, int number_digits) { @@ -162,23 +134,6 @@ track_add_wpt(route_head* rte, Waypoint* wpt, const QString& namepart, int numbe global_track_list->add_wpt(rte, wpt, false, namepart, number_digits); } -// FIXME: can we delete this unused and untested code? -#ifdef DEAD_CODE_IS_REBORN -Waypoint* -route_find_waypt_by_name(route_head* rh, const char* name) -{ - queue* elem, *tmp; - - QUEUE_FOR_EACH(&rh->waypoint_list, elem, tmp) { - Waypoint* waypointp = reinterpret_cast(elem); - if (waypointp->shortname == name) { - return waypointp; - } - } - return nullptr; -} -#endif - void route_del_wpt(route_head* rte, Waypoint* wpt) { @@ -301,44 +256,6 @@ track_sort(RouteList::Compare cmp) global_track_list->sort(cmp); } -// FIXME: can we delete this unused and untested code? -#ifdef DEAD_CODE_IS_REBORN -/* - * Move the entire track queue onto the route queue making no attempt - * at all to "fix" anything in the process. - */ -void -routes_to_tracks() -{ - queue* elem, *tmp; - - QUEUE_FOR_EACH(&my_route_head, elem, tmp) { - route_head* trk = (route_head*) elem; - dequeue(&trk->Q); - ENQUEUE_TAIL(&my_track_head, &trk->Q); - } -} -#endif - -// FIXME: can we delete this unused and untested code? -#ifdef DEAD_CODE_IS_REBORN -/* - * Same, but in opposite direction. - */ -void -tracks_to_routes() -{ - queue* elem, *tmp; - - QUEUE_FOR_EACH(&my_track_head, elem, tmp) { - route_head* trk = (route_head*) elem; - dequeue(&trk->Q); - ENQUEUE_TAIL(&my_route_head, &trk->Q); - } -} -#endif - - /* * This really makes more sense for tracks than routes. * Run over all the trackpoints, computing heading (course), speed, and @@ -477,7 +394,6 @@ route_head::route_head() : line_width(-1), session(curr_session()) { - QUEUE_INIT(&Q); QUEUE_INIT(&waypoint_list); }; @@ -489,16 +405,6 @@ route_head::~route_head() } } -RouteList::RouteList() : QueueList(&head, &head_ct) -{ - QUEUE_INIT(&head); -} - -int RouteList::count() const -{ - return head_ct; -} - int RouteList::waypt_count() const { return waypt_ct; @@ -511,24 +417,23 @@ int RouteList::waypt_count() const void RouteList::add_head(route_head* rte) { - ENQUEUE_TAIL(&head, &rte->Q); - ++head_ct; + this->append(rte); } void RouteList::del_head(route_head* rte) { waypt_ct -= rte->rte_waypt_ct; - dequeue(&rte->Q); + const int idx = this->indexOf(rte); + removeAt(idx); delete rte; - --head_ct; } void RouteList::insert_head(route_head* rte, route_head* predecessor) { - ENQUEUE_AFTER(&predecessor->Q, &rte->Q); - ++head_ct; + const int idx = this->indexOf(predecessor); + this->insert(idx + 1, rte); } // Synthesizing names based on the total number of waypoints in the RouteList makes @@ -564,9 +469,7 @@ RouteList::del_wpt(route_head* rte, Waypoint* wpt) void RouteList::common_disp_session(const session_t* se, route_hdr rh, route_trl rt, waypt_cb wc) { - queue* elem, *tmp; - QUEUE_FOR_EACH(&head, elem, tmp) { - const route_head* rhp = reinterpret_cast(elem); + foreach (const route_head* rhp, *this) { if (rhp->session == se) { if (rh) { (*rh)(rhp); @@ -582,29 +485,24 @@ RouteList::common_disp_session(const session_t* se, route_hdr rh, route_trl rt, void RouteList::flush() { - queue* elem, *tmp; - - QUEUE_FOR_EACH(&head, elem, tmp) { - queue* q = dequeue(elem); - delete reinterpret_cast(q); + foreach (route_head* rte, *this) { + delete rte; } - head_ct = 0; + clear(); waypt_ct = 0; } void RouteList::copy(RouteList** dst) const { - queue* elem, *tmp, *elem2, *tmp2; + queue* elem2, *tmp2; if (*dst == nullptr) { *dst = new RouteList; } const char RPT[] = "RPT"; - QUEUE_FOR_EACH(&head, elem, tmp) { - auto rte_old = reinterpret_cast(elem); - + foreach (const route_head* rte_old, *this) { route_head* rte_new = route_head_alloc(); rte_new->rte_name = rte_old->rte_name; rte_new->rte_desc = rte_old->rte_desc; @@ -624,34 +522,22 @@ RouteList::restore(RouteList* src) if (src == nullptr) { return; } - flush(); - QUEUE_MOVE(&head, &src->head); - head_ct = src->head_ct; - src->head_ct = 0; - waypt_ct = src->waypt_ct; + *this = *src; + src->clear(); src->waypt_ct = 0; } void RouteList::swap(RouteList& other) { - queue tmp_head; - QUEUE_MOVE(&tmp_head, &(other.head)); - QUEUE_MOVE(&(other.head), &(this->head)); - QUEUE_MOVE(&(this->head), &tmp_head); - - const auto tmp_head_ct = other.head_ct; - other.head_ct = this->head_ct; - this->head_ct = tmp_head_ct; - - const auto tmp_waypt_ct = other.waypt_ct; - other.waypt_ct = this->waypt_ct; - this->waypt_ct = tmp_waypt_ct; + const RouteList tmp_list = *this; + other = *this; + *this = tmp_list; } void RouteList::sort(Compare cmp) { - sortqueue(&head, cmp); + std::sort(begin(), end(), cmp); } diff --git a/sort.cc b/sort.cc index 1f28e243e..1c636411b 100644 --- a/sort.cc +++ b/sort.cc @@ -62,28 +62,19 @@ int SortFilter::sort_comp_wpt(const queue* a, const queue* b) } } -int SortFilter::sort_comp_rh_by_description(const queue* a, const queue* b) +bool SortFilter::sort_comp_rh_by_description(const route_head* a, const route_head* b) { - const route_head* x1 = reinterpret_cast(a); - const route_head* x2 = reinterpret_cast(b); - - return x1->rte_desc.compare(x2->rte_desc); + return a->rte_desc < b->rte_desc; } -int SortFilter::sort_comp_rh_by_name(const queue* a, const queue* b) +bool SortFilter::sort_comp_rh_by_name(const route_head* a, const route_head* b) { - const route_head* x1 = reinterpret_cast(a); - const route_head* x2 = reinterpret_cast(b); - - return x1->rte_name.compare(x2->rte_name); + return a->rte_name < b->rte_name; } -int SortFilter::sort_comp_rh_by_number(const queue* a, const queue* b) +bool SortFilter::sort_comp_rh_by_number(const route_head* a, const route_head* b) { - const route_head* x1 = reinterpret_cast(a); - const route_head* x2 = reinterpret_cast(b); - - return cmp(x1->rte_num, x2->rte_num); + return a->rte_num < b->rte_num; } int SortFilter::SortCompWptFunctor::operator()(const queue* a, const queue* b) diff --git a/sort.h b/sort.h index 9e42f7cf4..b724ecc32 100644 --- a/sort.h +++ b/sort.h @@ -108,9 +108,9 @@ private: }; int sort_comp_wpt(const queue* a, const queue* b); - static int sort_comp_rh_by_description(const queue* a, const queue* b); - static int sort_comp_rh_by_name(const queue* a, const queue* b); - static int sort_comp_rh_by_number(const queue* a, const queue* b); + static bool sort_comp_rh_by_description(const route_head* a, const route_head* b); + static bool sort_comp_rh_by_name(const route_head* a, const route_head* b); + static bool sort_comp_rh_by_number(const route_head* a, const route_head* b); class SortCompWptFunctor {